﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FacebookWrapper.ObjectModel;
using B12_Ex01_Guy_066473471_Erez_021616735.FacebookApplicationLogic;

namespace B12_Ex01_Guy_066473471_Erez_021616735.userInterfaceForWindowsInWinForm
{
    public class PictureFaceBookBox : Panel
    {
        Panel innerPanel = new Panel();
        Button buttenLike = new Button();
        Button buttenComment = new Button();
        PictureBoxLazy pictureBox = new PictureBoxLazy();
        Photo m_Photo = null;

        public PictureFaceBookBox(Photo i_Photo)
        {
            m_Photo = i_Photo;
            InitializeComponent();
            ifILikeThisPhoto();
        }
        
        public PictureFaceBookBox()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            SuspendLayout();

            Size = new System.Drawing.Size(180, 180);

            innerPanel.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(255)))));
            innerPanel.Controls.Add(this.buttenLike);
            innerPanel.Controls.Add(this.buttenComment);
            innerPanel.Dock = System.Windows.Forms.DockStyle.Bottom;
            innerPanel.Location = new System.Drawing.Point(0, 150);
            innerPanel.Size = new System.Drawing.Size(180, 30);
            innerPanel.TabIndex = 0;


            pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
            pictureBox.Location = new System.Drawing.Point(0, 0);
            pictureBox.Size = new System.Drawing.Size(180, 150);
            pictureBox.TabIndex = 1;
            pictureBox.TabStop = false;

            buttenLike.Location = new System.Drawing.Point(12, 4);
            buttenLike.Size = new System.Drawing.Size(75, 23);
            buttenLike.TabIndex = 0;
            buttenLike.Text = "Like";
            buttenLike.Dock = DockStyle.Left;
            buttenLike.UseVisualStyleBackColor = true;
            // 
            // button2
            // 
            buttenComment.Location = new System.Drawing.Point(93, 4);
            buttenComment.Size = new System.Drawing.Size(75, 23);
            buttenComment.TabIndex = 0;
            buttenComment.Text = "Comment";
            buttenComment.UseVisualStyleBackColor = true;
            buttenComment.Dock = DockStyle.Right;
            buttenLike.Click += buttonLike_Click;

            innerPanel.Controls.Add(this.buttenLike);
            innerPanel.Controls.Add(this.buttenComment);
            Controls.Add(this.pictureBox);
            Controls.Add(this.innerPanel);


            Location = new System.Drawing.Point(210, 308);

            TabIndex = 2;  
        }

        public void Load(string i_Url)
        {
            pictureBox.ImageLocation = i_Url;
        }

        public string ImageLocation
        {
            get { return pictureBox.ImageLocation; }
            set { pictureBox.ImageLocation = value; }
        }
       
        public PictureBoxSizeMode SizeMode 
        {
            get { return pictureBox.SizeMode; }
            set { pictureBox.SizeMode = value; }
        }

        private void buttonLike_Click(object sender, EventArgs e)
        {
            if (ifILikeThisPhoto() == true)
            {
                m_Photo.Unlike();
                buttenLike.Text = "Like";
            }
            else
            {
                m_Photo.Like();
                buttenLike.Text = "UnLike";
            }
            
        }

        private bool ifILikeThisPhoto()
        {
            bool retVal = false;
            User me = FacebookApplication2.Instance.LoggedInUser;
            var listLike = m_Photo.LikedBy.ToList();
            User result = m_Photo.LikedBy.Find(
             delegate(User bk)
             {
                 return bk.Id == me.Id;
             }
             );
            if (result != null)
            {
                buttenLike.Text = "UnLike";
                retVal = true;             
            }
            else
            {
                buttenLike.Text = "Like";
            }
            return retVal;
        }
    }

    public class PictureBoxLazy : PictureBox
    {
        public new void Load(string i_Url)
        {
            ImageLocation = i_Url;
        }

        public new string ImageLocation { get; set; }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);

            if (Image == null && ImageLocation != null)
            {
                LoadAsync(ImageLocation);
            }
        }
    }
}
